﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_Audit_Create : LoggedInPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        valEndDateFormat.ValueToCompare = DateTime.Today.ToShortDateString();
        txtEndDate.MaxDate = DateTime.Today;
        btnCreate.Click += new EventHandler(btnCreate_Click);
        ddlCountry.DataBinding += new EventHandler(ddlCountry_DataBinding);
        if (!IsPostBack)
        {
            for (int i = 0; i < 10; i++)
            {
                ddlNoPeople.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
            }
            //check the current audit dosnt exist
            CarbonExchange.Bll.AuditFactory auditFactory = new CarbonExchange.Bll.AuditFactory();
            if (auditFactory.GetCurrentForUserId(User.Identity.User.Id) != null)
            {
                AcmsContext.RedirectToUrl("~/MyCO2/ContinueAudit.aspx", this);
            }
            DataBind();
        }

    }

    void ddlCountry_DataBinding(object sender, EventArgs e)
    {
        CarbonExchange.Bll.CountryFactory factory = new CarbonExchange.Bll.CountryFactory();
        ddlCountry.DataValueField = "Id";
        ddlCountry.DataTextField = "Name";
        ddlCountry.DataSource = factory.GetAllBySiteId(AcmsContext.Site.Id).ToArray().OrderBy(x => x.Name);
    }

    void btnCreate_Click(object sender, EventArgs e)
    {
        if (ddlCountry.SelectedItem.Text.Replace(" ", "").Equals("unitedstatesofamerica", StringComparison.InvariantCultureIgnoreCase) ||
                ddlCountry.SelectedItem.Text.Replace(" ", "").Equals("usa", StringComparison.InvariantCultureIgnoreCase))
        {
            valStateReq.Enabled = true;
        }
        else
        {
            valStateReq.Enabled = false;
        }
        if (IsValid)
        {
            CarbonExchange.Bll.AuditFactory factory = new CarbonExchange.Bll.AuditFactory();
            CarbonExchange.Bll.Audit audit = new CarbonExchange.Bll.Audit(User.Identity.User.Id, txtEndDate.SelectedDate.Value);
            audit.CountryId = int.Parse(ddlCountry.SelectedValue);
            if (ddlCountry.SelectedItem.Text.Replace(" ", "").Equals("unitedstatesofamerica", StringComparison.InvariantCultureIgnoreCase) ||
                    ddlCountry.SelectedItem.Text.Replace(" ", "").Equals("usa", StringComparison.InvariantCultureIgnoreCase))
            {
                audit.State = ddlState.SelectedValue;
            }
            else
            {
                audit.State = null;
            }
            audit.PeopleInHousehold = int.Parse(ddlNoPeople.SelectedValue);
            factory.Save(audit);
            //redirect to first section
            //CarbonExchange.Bll.SectionFactory sectionFactory = new CarbonExchange.Bll.SectionFactory();
            //var section = sectionFactory.GetAll().FirstOrDefault();
            //var sectionId = "";
            //if (section != null)
            //{
            //    sectionId = section.Id.ToString();
            //}
            AcmsContext.RedirectToUrl("~/MyCO2/ContinueAudit.aspx", this);
        }
    }
}
